<?php
/**
 * Created by PhpStorm.
 * User: Gooraye
 * Date: 2016-08-09
 * Time: 11:33
 */
namespace Admin\Controller;
use Recruit\Api\UcenterMemberApi;
use Recruit\Api\CompanyApplyApi;
use Recruit\Api\CompanyEmployApi;
use Recruit\Api\UserResumeApi;

class EnterpriseController extends AdminController {
    /*
     * 企业信息
     * */
    public function info(){
        $map['uid'] = UID;
        $info = apiCall(UserResumeApi::GET_INFO,array($map));

        if($info['status']){
            $info['info']['content'] = htmlspecialchars_decode($info['info']['content']);

            $this->assign($info['info']);
        }
        $this->display();
    }
    /*
     * 收到的简历
     * */
    public function resume(){
        //得到公司的id找出投向公司的简历
        $idmap['itboye_company_apply.uid'] = UID;//公司id也指的是用户id
        //连表查询得到投向公司的简历信息
        $page = array('curpage'=>0,'size'=>10);
        $fields ='itboye_company_apply.resume_id,itboye_company_apply.create_time,itboye_company_apply.uid,itboye_company_apply.company_employ_id,itboye_user_resume.title,itboye_company_employ.title as cetitle';
        $info = apiCall(CompanyApplyApi::JION_PAGE,array($idmap,$page,'create_time desc',false,$fields));
        if($info['status']){
            $this->assign('list',$info['info']['list']);
            $this->assign('show',$info['info']['show']);
        }
        $this->display();
    }

    /*
     * 查看简历
     * */
    public function view(){
        $map['id'] = I('resume_id');
        $info = apiCall(UserResumeApi::GET_INFO,array($map));
        if($info['status']){
            $info['info']['content'] = htmlspecialchars_decode($info['info']['content']);
            $this->assign($info['info']);
        }
        $this->display();
    }

    /*
     * 岗位管理
     * */
    public function publish_job(){
        $idmap['uid'] = UID;
        $page = array('curpage'=>0,'size'=>10);

        $info = apiCall(CompanyEmployApi::QUERY,array($idmap,$page,'create_time desc'));
        if($info['status']){
            $this->assign('list',$info['info']['list']);
            $this->assign('show',$info['info']['show']);
        }
        $this->display();
    }
    /*
     * 发布的岗位
     * */
    public function add_job(){
        if(IS_POST){
            if(!empty(I('title')) && !empty(I('text'))){
                $entity = array(
                    'title' =>  I('title'),
                    'uid'   =>  UID,
                    'text'  =>  I('text'),
                    'create_time'=> time(),
                    'update_time'=> time(),
                    'logo' => I('main_img'),
                    'address' => I('address')
                );
                $info = apiCall(CompanyEmployApi::ADD,array($entity));
                if($info['status']){
                    $this->success('招聘发布成功',U('Enterprise/publish_job'));
                }else{
                    $this->error('招聘发布失败');
                }
            }else{
                $this->error('发布的信息缺失');
            }

        }else{
            $this->display();
        }
    }

    /*
     * 编辑岗位重新上传
     * */
    public function edit_job(){
        $map['id'] = I('id');
        if(IS_POST){

            $entity = array(
                'title' => I('title'),
                'address' => I('address'),
                'text' => I('text'),
                'update_time'=> time(),
                'logo' => I('main_img'),
            );
            $info = apiCall(CompanyEmployApi::SAVE_BYID,array($map['id'],$entity));
            if($info['info'] > 0){
                $this->success('修改成功',U('Enterprise/publish_job'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $info = apiCall(CompanyEmployApi::GET_INFO,array($map));
            if($info['status']){
                $this->assign($info['info']);
            }
            $this->display();
        }
    }

    /*
     * 查看发布的岗位
     * */
    public function view_employ(){
        $map['id'] = I('id');
        $info = apiCall(CompanyEmployApi::GET_INFO,array($map));
        if($info['status']){
            $info['info']['text'] = htmlspecialchars_decode($info['info']['text']);
            $this->assign($info['info']);
        }
        $this->display();
    }

    /*
     * 企业信息简历显示和添加
     * */
    public function company_resume(){
        if(IS_POST){
            $entiy =  array(
                'uid' => UID,
                'create_time' => time(),
                'update_time' => time(),
                'title' => I('title'),
                'content' => I('content')
            );
            $info = apiCall(UserResumeApi::ADD,array($entiy));
            if($info['status']){
                $this->success('添加成功',U('Enterprise/company_resume'));
            }else{
                $this->error('公司简历添加失败');
            }
        }else{
           $this->display();//添加简历
        }
    }

    /*
     * 企业简历重新编辑和上传
     * */
    public function edit_company_resume(){
        $id = I('id','');
        if(IS_POST){
            $map['id'] = $id;
            $entity = array(
                'update_time' => time(),
                'title' => I('title'),
                'content' => I('content'),
            );
            $info = apiCall(UserResumeApi::SAVE,array($map,$entity));
            if($info['status']){
                $this->success('修改成功',U('Enterprise/info'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $map['id'] = I('id');
            $info = apiCall(UserResumeApi::GET_INFO,array($map));
            if($info['status']){
                $info['info']['content'] = htmlspecialchars_decode($info['info']['content']);
                $this->assign($info['info']);
            }
            $this->display();
        }
    }

    /*
     * ajax异步删除发布的招聘信息
     * */
    public function delete_job(){
        $map['id'] = I('id');
        $map['uid'] = UID;
        $info = apiCall(CompanyEmployApi::DELETE,array($map));
        if($info['info'] > 0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}